﻿using System.Xml;
using System.ComponentModel;
using System.Windows.Forms;
using SdmxMl.Common;

namespace SdmxMl.Structure
{
    /// <summary>
    /// OrganisationSchemeType contains the details of an OrganisationScheme. In OrganisationSchemes,
    /// the organisation roles of agency, data provider, and data consumer may be specified.
    /// A single organisation may play more than one role. Name is an element which provides for
    /// a human-readable name for the organization. Description may be used to provide a longer,
    /// human-readable description. the is attribute provides a formal ID for the organisation scheme;
    /// the version attribute specifies a particular version. If blank, it is assumed that the version
    /// is "1.0". The uri attributre specifies the location of a valid SDMC Structure Message
    /// containing the full details of the organisation sc`heme, and is required if the
    /// isExternalReference attribute has a value of true. If isExternalReference has a value of false,
    /// full details must be provided in the current instance of the OrganisationScheme element.
    /// The urn attribute provides a formal SDMX Registry URL - see the Logical Registry Specification
    /// for specific requirements. An agencyID must be provided, identifying the maintenance agency
    /// of the organisation scheme. Also, if the organisation scheme is final, the isFinal attribute
    /// must have a value of true - otherwise, it will be assumed to be non-final. (All production
    /// schemes must be made final - that is, unchangeable without versioning.) The validFrom and
    /// validTo attributes provide inclusive dates for providing supplemental validity information
    /// about the version.
    /// </summary>
    public class OrganisationSchemesType: Artefact
    {
        #region Properties

        private AgenciesType agencyList;
        public AgenciesType Agencies
        {
            get { return agencyList; }
            set { agencyList = value; }
        }

        private DataProvidersType providersList;
        public DataProvidersType DataProviders
        {
            get { return providersList; }
            set { providersList = value; }
        }

        private DataConsumersType consumersList;
        public DataConsumersType DataConsumers
        {
            get { return consumersList; }
            set { consumersList = value; }
        }

        #endregion //Properties

        #region Constructors

        public OrganisationSchemesType()
            : base()
        {
            Init();
        }

        private void Init()
        {
            agencyList = new AgenciesType();
            consumersList = new DataConsumersType();
            providersList = new DataProvidersType();
        }


        public OrganisationSchemesType(XmlNode nodeArtefact, SdmxNamespaceManager nsmgr) 
            : base(nodeArtefact, nsmgr)
        {
            Init();
            agencyList.ReadXml(nodeArtefact, nsmgr, "./structure:Agencies/structure:Agency");
            providersList.ReadXml(nodeArtefact, nsmgr, "./structure:DataProviders/structure:DataProvider");
            consumersList.ReadXml(nodeArtefact, nsmgr, "./structure:DataConsumers/structure:DataConsumer");
        }
        #endregion //Constructors

        #region Tree

        private TreeNode FillOrganisationNodes(FlatHierarchicalList orgList, TreeNode parentNode)
        {
            TreeNode tc = new TreeNode(orgList.ToString(), 1, 1);
            tc.Tag = orgList;
            parentNode.Nodes.Add(tc);
            return orgList.FillTree(tc);
        }

        public override TreeNode FillNode(TreeNode parentNode)
        {
            TreeNode t = base.FillNode(parentNode);
            TreeNode tc = FillOrganisationNodes(agencyList, t);
            FillOrganisationNodes(providersList, t);
            FillOrganisationNodes(consumersList, t);
            return t;
        }
        #endregion //Tree

        #region Override

        public override void WriteXmlInsider(XmlTextWriter xw)
        {
            string prefix = "structure";
            if (agencyList.Count > 0)
            {
                xw.WriteStartElement(prefix, agencyList.ToString(), null);
                foreach (OrganisationType o in agencyList)
                    o.WriteXml(xw, prefix, "Agency");
                xw.WriteEndElement();
            }
            if (providersList.Count > 0)
            {
                xw.WriteStartElement(prefix, providersList.ToString(), null);
                foreach (OrganisationType o in providersList)
                    o.WriteXml(xw, prefix, "DataProvider");
                xw.WriteEndElement();
            }
            if (consumersList.Count > 0)
            {
                xw.WriteStartElement(prefix, consumersList.ToString(), null);
                foreach (OrganisationType o in consumersList)
                    o.WriteXml(xw, prefix, "DataConsumer");
                xw.WriteEndElement();
            }
        }
        #endregion //Override

        #region Artefact Type

        public override Manager.SdmxArtefactType ArType
        {
            get { return Manager.SdmxArtefactType.OrganisationSchemes; }
        }

        #endregion

    }
}
